<template>
  <n-modal
    v-model:show="modelValue.toMove"
    :mask-closable="false"
    preset="dialog"
    title="确认"
    content="你确认"
    positive-text="确认"
    negative-text="算了"
    @positive-click="onPositiveClick"
    @negative-click="modelValue.toMove = false"
  >
    <!--    {{modelValue}}{{originalField}}-->
    原来领域：{{ originalField }}，原来阶段：{{ originalStage }}
    <n-select
      v-model:value="modelValue.toSaveField"
      @update:value="modelValue.toSaveStageId = null"
      :options="fieldTree.getFieldSelectOptions()"
      style="width: 200px"
      filterable
    />
    <n-select
      v-model:value="modelValue.toSaveStageId"
      :options="stageOptions"
      style="width: 200px"
      filterable
    />
  </n-modal>
</template>
<script
  lang="ts"
  setup
  generic="T extends object & { toMove: boolean,taskId: number,type:number,state:string,originalStageId:number,toSaveStageId:number|null,toSaveField:number|undefined}"
>
import { TaskLogData } from "@model/TaskLogMap";
import { StageData, StageList } from "@model/stage";
import { SurfaceSetting } from "@model/surfaceSetting";
import UseCacheIdStatMap from "@pinias/UseCacheIdStatMap";
import UseCacheTaskLog from "@pinias/UseCacheTaskLog";
import UseFieldTree from "@pinias/UseFieldTree";
import UseStage from "@pinias/UseStage";
import UseSurfaceSetting from "@pinias/UseSurfaceSetting";
import useTask from "@pinias/UseTask";
import pinia from "@pinias/pinia";
import { DateTimeStr } from "@utils/DateUtils";
import { NModal, useMessage } from "naive-ui";
import { computed, ref, watch } from "vue";

// 全局数据
const { getSurfaceSetting } = UseSurfaceSetting(pinia);
const surfaceSetting: SurfaceSetting = getSurfaceSetting();
const { getStageList } = UseStage(pinia);
const stageList: StageList = getStageList();
const { getTaskList } = useTask(pinia);
const taskList = getTaskList();
const { getCacheIdStatMap } = UseCacheIdStatMap(pinia);
const cacheIdStatMap = getCacheIdStatMap();
const { getCacheTaskLogMap } = UseCacheTaskLog(pinia);
const cacheTaskLogMap = getCacheTaskLogMap();
const { getFieldTree } = UseFieldTree(pinia);
const fieldTree = getFieldTree();
const message = useMessage();

// 父组件数据
const props = defineProps<{ modelValue: T }>();
const emit = defineEmits(["update:modelValue"]);
const modelValue = computed({
  get: () => props.modelValue,
  set: val => emit("update:modelValue", val),
});
const onPositiveClick = async () => {
  // message.success("Submit");
  if (!(modelValue.value.toSaveStageId > 0)) {
    message.error("请选择阶段");
    return;
  }
  // 切换领域时，更新对应领域的统计情况：
  if (modelValue.value.originalStageId !== modelValue.value.toSaveStageId) {
    // 统计情况：找到taskId，加入其中；然后找到父id，加入其中
    const stageIdStat = await cacheIdStatMap.getIdStatNotCreate(modelValue.value.taskId);
    // console.log("stageIdStat",stageIdStat)
    const taskLogDatas = new Array<TaskLogData>();
    if (stageIdStat !== undefined) {
      for (let value of stageIdStat.everyDayLogMap.values()) {
        for (let i = 0; i < value.length; i++) {
          const valueKey = value[i];
          const timeStamp = DateTimeStr.getTimeStamp(valueKey);
          // console.log("timeStamp",timeStamp,valueKey,value)
          if (timeStamp > 0) {
            const taskLogMap = await cacheTaskLogMap.getTaskLogMap(timeStamp);
            const taskLogData = taskLogMap.taskLogMap.get(valueKey);
            // console.log("taskLogMap",taskLogMap,taskLogData)
            if (taskLogData !== undefined) taskLogDatas.push(taskLogData);
          }
        }
      }
    }
    // console.log("taskLogDatas",taskLogDatas)
    if (taskLogDatas.length > 0) {
      await cacheIdStatMap.changeTaskLogData4Stat(
        taskLogDatas,
        [modelValue.value.originalStageId, originalFieldId.value],
        taskLogDatas,
        [modelValue.value.toSaveStageId, modelValue.value.toSaveField],
      );
    }
  }
  // 切换stage的任务id
  StageData.changeTask2NewStage(
    modelValue.value.toSaveStageId,
    modelValue.value.originalStageId,
    modelValue.value.taskId,
    modelValue.value.type,
    modelValue.value.state,
    stageList,
  );
  // 更改taskId的fatherId
  const taskData = taskList.getTaskDataById(modelValue.value.taskId);
  if (taskData !== undefined) taskData.fatherId = modelValue.value.toSaveStageId;
  // tableData.value = tableData.value.filter(item => item.id !== id);
  modelValue.value.toMove = false;
  surfaceSetting.TaskListKey = new Date().getTime();
  surfaceSetting.taskBoardList.key = new Date().getTime() + 10;
};

// 任务栏选项
const originalField = ref<string | undefined>(undefined);
const originalFieldId = ref<number | undefined>(undefined);
const originalStage = ref<string | undefined>(undefined);
const stageOptions = ref<Array<{ label: string; value: number }>>([]);
watch(modelValue.value, () => {
  if (modelValue.value.toMove) {
    let stage = stageList.getStageDataById(modelValue.value.originalStageId);
    if (stage !== undefined) {
      originalStage.value = stage.name;
      const field = fieldTree.getFieldDataById(stage.fatherId);
      originalFieldId.value = field?.id;
      originalField.value = field?.topic;
    }
    if (modelValue.value.toSaveStageId > 0) {
      stage = stageList.getStageDataById(modelValue.value.toSaveStageId);
      modelValue.value.toSaveField = stage?.fatherId;
    }
    const field = fieldTree.getFieldDataById(modelValue.value.toSaveField);
    stageOptions.value = stageList.getStageSelectOptions(field);
    // console.log(stageOptions.value,field);
  }
});
</script>
